#include "led.h"
#include "monake.h"
#include "wk_usart.h"



static uint8_t monake_heart[8] = {0x01,0x03,0x9c,0x41,0x00,0x03,0x7b,0x8f};

static uint8_t monake_recvdata_calibration(uint8_t* buf, uint32_t len, uint8_t* s_buf, uint32_t* s_len)
{
	uint16_t xcrc;
	uint16_t crc_value = 0xFFFF;
	MONAKE_DATA_t* p_data;
	
	p_data = (MONAKE_DATA_t*)buf;
	if((p_data->ADDR != 0x01) || (p_data->CMD != 0x03) || (p_data->LEN >= (20-5))) {
		printf("cmd err\r\n");
		return 1;
	}
	xcrc = ((uint16_t)buf[len-1]<<8) | (uint16_t)buf[len-2];
	
	for(uint8_t i = 0; i < (p_data->LEN+3); i++) {
		crc_value ^= buf[i];
		for(uint8_t j = 0; j < 8; j++) {
			if(crc_value & 0x0001) {
				crc_value = (crc_value >>1) ^ 0xa001;
			} else {
				crc_value=crc_value >> 1;
			}
		}
	}
	
	if(crc_value != xcrc) {
		printf("sum err\r\n");
		return 1;
	}
	
	return 0;
}

static void monake_cmd_parse(uint8_t* buf, uint32_t len)
{
	MONAKE_DATA_t* p_data;
	
	p_data = (MONAKE_DATA_t*)buf;
	if(p_data->CMD == 0x03) {
		lsd_t.run_state = p_data->DATA[3];
		lsd_t.door_state = p_data->DATA[5];
		
	}
}





/********************************************************************************************/
/********************************************************************************************/
/********************************************************************************************/
/********************************************************************************************/
/********************************************************************************************/
/********************************************************************************************/
/********************************************************************************************/
/********************************************************************************************/

PROTO_pt monake_ptu;


void monake_soft_ware_init(void)
{
	wk_usart1_init();
	monake_ptu = protocol_stack_init("monake", 4, 40, 4, 40, 4, 40);
	if(monake_ptu != NULL) {
		printf("monake malloc ok\r\n");
		monake_ptu->send_ctrol_t.default_inter = 10;
		monake_ptu->recvdata_calibration_fn = monake_recvdata_calibration;
		monake_ptu->send_data_fn = rs485_send_data;
		
	}
}



void monake_recvdata_deal(void)
{
	uint32_t recv_len;
	uint8_t recv_buf[20];
	
	recv_len = msg_data_out_queue(recv_485_buf, recv_buf);
	if(recv_len > 0) {
		protocol_recv_data_in_recv_queue(monake_ptu, recv_buf, recv_len);
	}
	
	
}

void monake_recvdata_parse(void)
{
	uint32_t recv_len;
	uint8_t recv_buf[20];
	
	recv_len = msg_data_out_queue(monake_ptu->recv_queue, recv_buf);
	if(recv_len > 0) {
		monake_cmd_parse(recv_buf, recv_len);
		
	}
	
}

void monake_senddata_packaged(void)
{
	uint8_t data[40];

	DATA_t* p_data = (DATA_t*)data;
	
	p_data->reissue = 0;
	p_data->re_cnt = 0;
	p_data->interval = 0;
	p_data->length = 8;
	memcpy(p_data->data, monake_heart, 8);
	msg_data_back_equeue(monake_ptu->send_queue,data,8+16);
	
}

void monake_senddata_transmit(void)
{
	
	protocol_send_data_out_queue(monake_ptu);
}

